package com.lau.demo;

import com.lau.utils.AsyncMethodInvokerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author:Lau
 * @create: 2024-03-22 17:21
 * @Description: 测试
 */
@Component
@Slf4j
public class Demo {

    @Autowired
    private AsyncMethodInvokerUtils asyncMethodInvoker;


    @Scheduled(cron = "*/10 * * * * *  ")
    public void test() throws ExecutionException, InterruptedException {
        for (int i = 0; i <50 ; i++) {
            log.info("--------{}-----------",i);
            CompletableFuture<Object> future = asyncMethodInvoker.invokeAsync(Demo.class, "testAsycn", "aaaa", i);
//            while (!future.isDone()){
//                log.info("等待中......");
//                Thread.sleep(100); // 100毫秒
//            }
//            log.info("-----test---result:{}",future.get());
        }
    }

    public String testAsycn(String name,Integer code){
        log.info("参数1：{}，2：{}",name,code);
        log.info("线程名{}",Thread.currentThread().getName());
        try {
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Integer.toString(code);
    }
}
